package w83b.w83bOperacionesPaso.w83bBandejaEntrada;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import w83b.w83bBeans.W83bPreavisosBean;
import w83b.w83bBeans.W83bUsuarioXLNetsBean;
import w83b.w83bUtilidades.W83bUtilidadesComunes;
import w83b.w83bUtilidadesComunes.W83bClsConstantes;
import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;

// TODO: Auto-generated Javadoc
/**
 * The Class W83bTablaAgrupacionesOP.java.
 *
 * @author Deusto Sistemas
 */
public class W83bBusqBandejaEntrOPAction extends Q70ActionPasoOP{

	
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		//Guardamos la accion en el contexto
			contexto.set("action",request.getParameter("action"));
			W83bPreavisosBean preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
			String idioma = (String)contexto.get(W83bClsConstantes.CONSTANTE_CONTEXTO_IDIOMA);
			preavisosBean.setIdioma(idioma);
			W83bUsuarioXLNetsBean userBean = (W83bUsuarioXLNetsBean)contexto.get(W83bClsConstantes.CONSTANTE_CONTEXTO_USUARIO);
			preavisosBean.setCodTerritorio(userBean.getCodTerritorioDelegacion());
			
			if((StringUtils.isEmpty((String)contexto.get("modoPantalla"))
					||W83bClsConstantes.MODO_BUSQUEDA.equals(contexto.get("modoPantalla")))
					&&(!"sord".equals(request.getParameter("action")))){
					
				if("true".equals(request.getParameter(W83bClsConstantes.RIA_SEARCH))){
					preavisosBean= new W83bPreavisosBean();
					preavisosBean.setIdioma((String)contexto.get(W83bClsConstantes.CONSTANTE_CONTEXTO_IDIOMA));
					preavisosBean.setCodTerritorio(((W83bUsuarioXLNetsBean)contexto.get(W83bClsConstantes.CONSTANTE_CONTEXTO_USUARIO)).getCodTerritorioDelegacion());
					preavisosBean.setEstado(request.getParameter("w83bPreavisosBean.estado"));			
					preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);
					contexto.set("w83bPreavisosBean",preavisosBean);
				}
				else
				{
					preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);
					contexto.set("w83bPreavisosBean",preavisosBean);
				}
			}else if("sord".equals(request.getParameter("action"))){
				preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
				preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);			
				contexto.set("w83bPreavisosBean",preavisosBean);
			}else if(StringUtils.isEmpty(((W83bPreavisosBean)contexto.get("w83bPreavisosBean")).getSort())){
				preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
				preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);			
				contexto.set("w83bPreavisosBean",preavisosBean);
			}else if(W83bClsConstantes.MODO_BUSQUEDA_CONBUSQ_INICIAL.equals(contexto.get("modoPantalla"))){
				preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
				preavisosBean = (W83bPreavisosBean)W83bUtilidadesComunes.inicializarPaginador(request, preavisosBean);			
				contexto.set("w83bPreavisosBean",preavisosBean);
			}
		
	}

	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String action = (String)contexto.get("action");

		W83bPreavisosBean preavisosBean = (W83bPreavisosBean)contexto.get("w83bPreavisosBean");
		
		if(null==(ArrayList)contexto.get("w83bListaPreavisos")){	
			contexto.set("w83bListaPreavisos", new ArrayList());
		}
		if(preavisosBean.isCambioPagina()&&!("sord").equals(action)){
			return mapping.findForward("outOP");
		}
		if((StringUtils.isEmpty(request.getParameter("lanzarBusqueda")))){
			contexto.set("modoPantalla",W83bClsConstantes.MODO_BUSQUEDA);
			return mapping.findForward("outOP");
		}
		else
		{
			if (("find").equals(action)
					||"sord".equals(action)){
				//Si vengo de una pantalla anterior tengo que mirar si he realizado una búsqueda anterior
				if(StringUtils.isEmpty((String)contexto.get("modoPantalla"))
						||W83bClsConstantes.MODO_BUSQUEDA.equals(contexto.get("modoPantalla"))){
					return mapping.findForward("find");
				}else{
					contexto.set("modoPantalla",W83bClsConstantes.MODO_BUSQUEDA);
					if("true".equals((String)contexto.get("busqRealizada"))){
						return mapping.findForward("find");					
					}
				}
			}
		}
		

		
		return mapping.findForward("ok");
	}

}

